<template>
    <el-dialog v-model="visible" :title="title" width="500px">
        <el-form :model="formData" ref="formRef" :rules="rules" label-width="80px">
            <el-form-item label="父级部门">
                <el-tree-select
                    v-model="formData.parentId"
                    :data="deptTree"
                    :props="treeProps"
                    check-strictly
                    clearable
                    :render-after-expand="false"
                    node-key="id"
                    placeholder="请选择父级部门"
                />
            </el-form-item>
            <el-form-item label="部门名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入部门名称" />
            </el-form-item>
            <el-form-item label="部门领导" prop="leaderId">
                <el-select v-model="formData.leaderId" placeholder="请选择部门领导">
                    <el-option v-for="item in users" :key="item.id" :label="item.realName" :value="item.id" />
                </el-select>
            </el-form-item>
            <el-form-item label="排序">
                <el-input-number v-model="formData.sort" :min="1"/>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">取消</el-button>
                <el-button type="primary" @click="addOrUpdate">确认</el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import {addReq,updateReq} from '@/api/sys/dept/index.js'

const visible = ref(false)

const formData = reactive({
    id:'',
    parentId: '',
    name: '',
    sort: 1,
    leaderId: '',
    remark:''
})

const rules = reactive({
    name: [{ required: true, message: '请输入部门名称', trigger: 'blur' } ],
})

const props = defineProps({
    querySelective: Function,
    deptTree: Array,
    users: Array
})

const title = ref('')
const actionType = ref('')
const open = (type, row) => {
    if (type == 'add') {
        actionType.value = type
        title.value = '添加部门'
        formData.id = ''
        formData.parentId =  ''
        formData.name = ''
        formData.sort = 1
        formData.leaderId = ''
        formData.remark = ''
        formRef.value?.resetFields()
    }
    if (type == 'update') {
        actionType.value = type
        title.value = '修改部门'
        formData.id = row.id
        formData.parentId = (row.parentId == '0' ? '' : row.parentId)
        formData.name = row.name
        formData.sort = row.sort
        formData.leaderId = row.leaderId
        formData.remark = row.remark
    }
    visible.value = true;
}

defineExpose({
    open
})


const close = () => {
    visible.value = false;
}

const formRef = ref()

const addOrUpdate = async () => {
    const valid = await formRef.value?.validate() // 改变这一行
    if (valid) {
        formData.parentId = (formData.parentId == '' ? '0' : formData.parentId)
        if (actionType.value == 'add') {
            const res = await addReq(formData);
            if (res.code == 200) {
                // 添加成功！
                ElMessage.success(res.message)
                // 查询列表
                props.querySelective();
                close();
            }
        }
        if (actionType.value == 'update') {
            const res = await updateReq(formData);
            if (res.code == 200) {
                // 添加成功！
                ElMessage.success(res.message)
                // 查询列表
                props.querySelective();
                close();
            }
        }
    }
}

const treeProps = ref({
    label:"name"
})
</script>